//
//  WDPContactCell.m
//  WDLoan
//
//  Created by gadmyth on 11/15/17.
//  Copyright © 2017 wanda. All rights reserved.
//

#import "WDPContactCell.h"
#import "WDPContactDataModel.h"

@interface WDPContactCell ()
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *phoneNumberLabel;
@property (nonatomic, strong) UIImageView *selectImageView;
@property (nonatomic, strong) WDPContactDataModel *model;
@end

@implementation WDPContactCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setBackgroundColor:[UIColor whiteColor]];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    _nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _nameLabel.font = FONT_PINGFANG_REGULAR_(16);
    _nameLabel.textColor = ColorFromRGB(0x333333);
    [self.contentView addSubview:_nameLabel];
    
    _nameLabel.font = [UIFont boldSystemFontOfSize:_nameLabel.font.pointSize];
    
    _phoneNumberLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _phoneNumberLabel.font = FONT_PINGFANG_REGULAR_(14);
    _phoneNumberLabel.textColor = ColorFromRGB(0x666666);
    [self.contentView addSubview:_phoneNumberLabel];
    
    _selectImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:_selectImageView];

    _selectImageView.image = [UIImage imageNamed:@"icon_contact_unselected"];
    _selectImageView.highlightedImage = [UIImage imageNamed:@"icon_contact_selected"];
    
    [self updateViews];
}

- (void)updateViewsWithData:(WDPContactDataModel *)model {
    _model = model;
    
    _nameLabel.text = model.name;
    _phoneNumberLabel.text = model.originPhone;
}

- (void)updateViews {
    [_selectImageView sizeToFit];
    [_selectImageView alignToParent:WDAlignCenterVertical margin:0.0f];
    [_selectImageView alignToParent:WDAlignLeft margin:15.0f];
    
    [_nameLabel sizeToFit];
    [_nameLabel alignToParent:WDAlignTop margin:11.0f];
    [_nameLabel rightTo:_selectImageView margin:15.0f];
    
    [_phoneNumberLabel sizeToFit];
    [_phoneNumberLabel alignTo:_nameLabel type:WDAlignLeft margin:0.0f];
    [_phoneNumberLabel bottomTo:_nameLabel margin:3.0f];
}

- (void)select {
    _selectImageView.highlighted = YES;
    
    _nameLabel.textColor = ColorFromRGB(0x00a1f4);
    _phoneNumberLabel.textColor = ColorFromRGB(0x00a1f4);
}

- (void)deSelect {
    _selectImageView.highlighted = NO;
    
    _nameLabel.textColor = ColorFromRGB(0x333333);
    _phoneNumberLabel.textColor = ColorFromRGB(0x666666);
}

@end
